import { _decorator, Component, EventMouse, EventTouch, Input, input, UITransform, Node } from "cc";
import { ItemUI } from "./ItemUI";
import { SlotHolder } from "./SlotHolder";
import { InventoryManager } from "./InventoryManager";
const { ccclass, property } = _decorator;

export class DragData {

    originHolder: SlotHolder

    originParent: Node

}


@ccclass('DragItem')
export class DragItem extends Component {

    currentItemUI: ItemUI

    currentHolder: SlotHolder

    targetHolder: SlotHolder

    protected onLoad(): void {
        this.currentItemUI = this.getComponent(ItemUI)
        this.currentHolder = this.node.parent.getComponent(SlotHolder)

        input.on(Input.EventType.MOUSE_DOWN, this.onTouchStart, this);
        input.on(Input.EventType.MOUSE_MOVE, this.onTouchMove, this);
        input.on(Input.EventType.MOUSE_UP, this.onTouchEnd, this);
    }

    onDestroy() {
        input.off(Input.EventType.MOUSE_DOWN, this.onTouchStart, this);
        input.off(Input.EventType.MOUSE_MOVE, this.onTouchMove, this);
        input.off(Input.EventType.MOUSE_UP, this.onTouchEnd, this);
    }

    onTouchStart() {
        InventoryManager.Instance.currentDrag = new DragData()
        InventoryManager.Instance.currentDrag.originHolder = this.node.parent.getComponent(SlotHolder)
        InventoryManager.Instance.currentDrag.originParent = this.node.parent;

        // TODO 记录原始数据
        this.node.setParent(InventoryManager.Instance.dragCanvas, true)
    }

    onTouchMove(event: EventMouse) {
        // console.log(event.getLocation());  // Location on screen space
        // console.log(event.getUILocation());  // Location on UI space
        this.node.setPosition(event.getLocation().x, event.getLocation().y)
    }

    onTouchEnd(event: EventMouse) {
        // if(isPointerOverGameObject){
        if (InventoryManager.Instance.CheckInActionUI(event.getLocationInView()) ||
            InventoryManager.Instance.CheckInInventoryUI(event.getLocationInView()) ||
            InventoryManager.Instance.CheckInEquipmentUI(event.getLocationInView())
        ) {
            // TODO this.targetHolder = 


        }
        // }

    }

}